<?php
declare(strict_types=1);

namespace App\CallBacks;

use App\App;
use App\GlobalData;
use App\Runtime;
use Swoole\Thread;
use Swoole\Timer;

class WorkerStart
{
    public function handle(\Swoole\Server $serv, int $wid): void
    {
        // 这是主的work线程 处理放这里吧  后面不行 再新开一个线程 处理
        if ($wid == 0) {
            Runtime::print("WorkerStart [$wid] tick [cons clear]");
            Timer::tick(5000, function () {
                $checkTime = time();
                foreach (GlobalData::getMap()['wait_cons']->toArray() as $fd => $connectTime) {
                    if ($checkTime - $connectTime > 60) {
                        Runtime::print("fd wait timeout close [$fd]");
                        App::getApp()->server->getServer()->close(intval($fd));
                    }
                }
            });
        }
//        var_dump(GlobalData::getMap());
        printf("Work Thread [%d] start \n", Runtime::getThreadId());
    }
}